package com.gxh.text.controller;


import com.gxh.text.entity.Tree;
import com.gxh.text.entity.TreseShop;
import com.gxh.text.entity.vo.TimeCs;
import com.gxh.text.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class  TreeController {
    @Autowired
    private TreeService ts;
    @PostMapping("/findlist")
    @ResponseBody
   public List findList(Tree tree){
      return  ts.findList(tree);


   }

    @PostMapping("/selecttree")
    @ResponseBody
    public List SelectTree(TreseShop treseShop){
        return  ts.SelectTree(treseShop);


    }

    /**
     * 查询当前日期前七天的日期
     * @param time
     * @return
     */

    @PostMapping("/stime")
    @ResponseBody
    public static ArrayList<String> pastDay(String time){
        ArrayList<String> pastDaysList = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            for (int i = 1; i <=7; i++) {
                pastDaysList.add(getPastDate(i,date));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        return pastDaysList;
    }

   public static String getPastDate(int past, Date date){
        //初始化日历对象
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return  result;
    }




}
